package com.GamersCom.model;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.GamersCom.db.Genre;
import com.GamersCom.db.GenreDAO;

public class GenreMgr
{
	GenreDAO dao = new GenreDAO();
	
	public void insert(Genre genre) throws SQLException
	{
		String query = "insert into genre (name) values (?)";
		List paramList = new ArrayList();
		paramList.add(genre.getName());
		
		dao.insert(query, paramList);
	}

	public void delete(Genre genre) throws SQLException 
	{
		String query = "delete from genre where name = ?";
		List paramList = new ArrayList();
		paramList.add(genre.getName());
		
		dao.delete(query, paramList);
	}
	
	public void update(Genre genre, Genre genreNew) throws SQLException 
	{
		String query = "update genre set name = ? where name = ?";
		List paramList = new ArrayList();
		paramList.add(genreNew.getName());
		paramList.add(genre.getName());
		
		dao.update(query, paramList);
	}
	
	public List<Genre> getAll() throws SQLException
	{
		String query = "select * from genre";		
		return dao.getAll(query);
	}
	
	public Genre getGenreByName(String name) throws SQLException
	{
		String query = "select * from genre where name = ?";
		List paramList = new ArrayList();
		paramList.add(name);
		
		return dao.getGenre(query, paramList);
	}
	
	public boolean nameExists(String name) 
	{
		try
		{
			if ( this.getGenreByName(name) == null)
				return false;
		}
		catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return true;
	}
	
}
